package com.ibeeking.found.cms.b.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.cms.b.rest.service.IPageComponentsService;
import com.ibeeking.found.cms.service.common.dos.PageComponentsDO;
import com.ibeeking.found.cms.service.common.param.PageComponentsParam;
import com.ibeeking.found.cms.service.common.query.PageComponentsQuery;
import com.ibeeking.found.cms.service.common.vo.PageComponentsVO;
import com.ibeeking.found.cms.service.mapper.PageComponentsMapper;
import com.ibeeking.nematos.core.utils.SpringBeanUtil;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.id.IdGenerator;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName PageComponentsServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class PageComponentsServiceImpl extends ServiceImpl<PageComponentsMapper, PageComponentsDO> implements IPageComponentsService {

    @Resource
    private PageComponentsMapper pageComponentsMapper;

    @Override
    public List<PageComponentsVO> list(PageComponentsQuery query) {
        List<PageComponentsDO> entityDOS = pageComponentsMapper.selectList(new LambdaQueryWrapper<PageComponentsDO>()
                .eq(query.getId() != null, PageComponentsDO::getId, query.getId())
                .eq(query.getGroupId() != null, PageComponentsDO::getGroupId, query.getGroupId())
                .eq(StringUtils.isNotBlank(query.getIndustry()), PageComponentsDO::getIndustry, query.getIndustry())
                .eq(StringUtils.isNotBlank(query.getCode()), PageComponentsDO::getCode, query.getCode())
                .like(StringUtils.isNotBlank(query.getName()), PageComponentsDO::getName, query.getName())
                .orderByDesc(PageComponentsDO::getSort)
        );
        List<PageComponentsVO> entityVOS =JsonUtils.jsonToList(entityDOS, PageComponentsVO.class);
        return entityVOS;
    }

    @Override
    public List<PageComponentsVO> queryAll() {
        List<PageComponentsDO> entityDOS = pageComponentsMapper.selectList(new LambdaQueryWrapper<PageComponentsDO>());
        return JsonUtils.jsonToList(entityDOS, PageComponentsVO.class);
    }

    @Override
    public List<PageComponentsVO> queryByGroupId(Long groupId) {
        PageComponentsQuery query = new PageComponentsQuery();
        query.setGroupId(groupId);
        return getBean().list(query);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(PageComponentsParam param) {
        PageComponentsDO entityDO = BeanUtil.convertBean(param, PageComponentsDO.class);
        entityDO.setId(IdGenerator.getId());
        return pageComponentsMapper.insert(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(PageComponentsParam param) {
        PageComponentsDO entityDO = BeanUtil.convertBean(param, PageComponentsDO.class);
        return pageComponentsMapper.updateById(entityDO) > 0;
    }

    @Override
    public PageComponentsVO view(Long id) {
        PageComponentsDO entityDO = pageComponentsMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        PageComponentsVO entityVO = BeanUtil.convertBean(entityDO, PageComponentsVO.class);
        return entityVO;
    }

    private IPageComponentsService getBean(){
        return SpringBeanUtil.getBean(this.getClass());
    }
}
